home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / patch / backupfile.c next >
C/C++ Source or Header  |  1994-08-01  |  8KB  |  340 lines

  1. /* backupfile.c -- make Emacs style backup file names
  2.    Copyright (C) 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it without restriction.
  6.  
  7.    This program is distributed in the hope that it will be useful,
  8.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  */
  10.  
  11. /* David MacKenzie <djm@ai.mit.edu>.
  12.    Some algorithms adapted from GNU Emacs. */
  13.  
  14. #include <stdio.h>
  15. #include <ctype.h>
  16. #include <sys/types.h>
  17. #include "backupfile.h"
  18. #include "config.h"
  19. char *index ();
  20. char *rindex ();
  21.  
  22. #ifdef DIRENT
  23. #include <dirent.h>
  24. #ifdef direct
  25. #undef direct
  26. #endif
  27. #define direct dirent
  28. #define NLENGTH(direct) (strlen((direct)->d_name))
  29. #else /* !DIRENT */
  30. #define NLENGTH(direct) ((direct)->d_namlen)
  31. #ifdef USG
  32. #ifdef SYSNDIR
  33. #include <sys/ndir.h>
  34. #else /* !SYSNDIR */
  35. #include <ndir.h>
  36. #endif /* !SYSNDIR */
  37. #else /* !USG */
  38. #ifdef SYSDIR
  39. #include <sys/dir.h>
  40. #endif /* SYSDIR */
  41. #endif /* !USG */
  42. #endif /* !DIRENT */
  43.  
  44. char *malloc ();
  45.  
  46. #ifndef isascii
  47. #define ISDIGIT(c) (isdigit ((unsigned char) (c)))
  48. #else
  49. #define ISDIGIT(c) (isascii (c) && isdigit (c))
  50. #endif
  51.  
  52. #if defined (HAVE_UNISTD_H)
  53. #include <unistd.h>
  54. #endif
  55.  
  56. #if defined (_POSIX_VERSION)
  57. /* POSIX does not require that the d_ino field be present, and some
  58.    systems do not provide it. */
  59. #define REAL_DIR_ENTRY(dp) 1
  60. #else
  61. #define REAL_DIR_ENTRY(dp) ((dp)->d_ino != 0)
  62. #endif
  63.  
  64. /* Which type of backup file names are generated. */
  65. enum backup_type backup_type = none;
  66.  
  67. /* The extension added to file names to produce a simple (as opposed
  68.    to numbered) backup file name. */
  69. char *simple_backup_suffix = "~";
  70.  
  71. char *basename ();
  72. char *dirname ();
  73. static char *concat ();
  74. char *find_backup_file_name ();
  75. static char *make_version_name ();
  76. static int max_backup_version ();
  77. static int version_number ();
  78.  
  79. #ifndef NODIR
  80. /* Return the name of the new backup file for file FILE,
  81.    allocated with malloc.  Return 0 if out of memory.
  82.    FILE must not end with a '/' unless it is the root directory.
  83.    Do not call this function if backup_type == none. */
  84.  
  85. char *
  86. find_backup_file_name (file)
  87.      char *file;
  88. {
  89.   char *dir;
  90.   char *base_versions;
  91.   int highest_backup;
  92.  
  93.   if (backup_type == simple)
  94.     return concat (file, simple_backup_suffix);
  95.   base_versions = concat (basename (file), ".~");
  96.   if (base_versions == 0)
  97.     return 0;
  98.   dir = dirname (file);
  99.   if (dir == 0)
  100.     {
  101.       free (base_versions);
  102.       return 0;
  103.     }
  104.   highest_backup = max_backup_version (base_versions, dir);
  105.   free (base_versions);
  106.   free (dir);
  107.   if (backup_type == numbered_existing && highest_backup == 0)
  108.     return concat (file, simple_backup_suffix);
  109.   return make_version_name (file, highest_backup + 1);
  110. }
  111.  
  112. /* Return the number of the highest-numbered backup file for file
  113.    FILE in directory DIR.  If there are no numbered backups
  114.    of FILE in DIR, or an error occurs reading DIR, return 0.
  115.    FILE should already have ".~" appended to it. */
  116.  
  117. static int
  118. max_backup_version (file, dir)
  119.      char *file, *dir;
  120. {
  121.   DIR *dirp;
  122.   struct direct *dp;
  123.   int highest_version;
  124.   int this_version;
  125.   int file_name_length;
  126.   
  127.   dirp = opendir (dir);
  128.   if (!dirp)
  129.     return 0;
  130.   
  131.   highest_version = 0;
  132.   file_name_length = strlen (file);
  133.  
  134.   while ((dp = readdir (dirp)) != 0)
  135.     {
  136.       if (!REAL_DIR_ENTRY (dp) || NLENGTH (dp) <= file_name_length)
  137.     continue;
  138.       
  139.       this_version = version_number (file, dp->d_name, file_name_length);
  140.       if (this_version > highest_version)
  141.     highest_version = this_version;
  142.     }
  143.   closedir (dirp);
  144.   return highest_version;
  145. }
  146.  
  147. /* Return a string, allocated with malloc, containing
  148.    "FILE.~VERSION~".  Return 0 if out of memory. */
  149.  
  150. static char *
  151. make_version_name (file, version)
  152.      char *file;
  153.      int version;
  154. {
  155.   char *backup_name;
  156.  
  157.   backup_name = malloc (strlen (file) + 16);
  158.   if (backup_name == 0)
  159.     return 0;
  160.   sprintf (backup_name, "%s.~%d~", file, version);
  161.   return backup_name;
  162. }
  163.  
  164. /* If BACKUP is a numbered backup of BASE, return its version number;
  165.    otherwise return 0.  BASE_LENGTH is the length of BASE.
  166.    BASE should already have ".~" appended to it. */
  167.  
  168. static int
  169. version_number (base, backup, base_length)
  170.      char *base;
  171.      char *backup;
  172.      int base_length;
  173. {
  174.   int version;
  175.   char *p;
  176.   
  177.   version = 0;
  178.   if (!strncmp (base, backup, base_length) && ISDIGIT (backup[base_length]))
  179.     {
  180.       for (p = &backup[base_length]; ISDIGIT (*p); ++p)
  181.     version = version * 10 + *p - '0';
  182.       if (p[0] != '~' || p[1])
  183.     version = 0;
  184.     }
  185.   return version;
  186. }
  187.  
  188. /* Return the newly-allocated concatenation of STR1 and STR2.
  189.    If out of memory, return 0. */
  190.  
  191. static char *
  192. concat (str1, str2)
  193.      char *str1, *str2;
  194. {
  195.   char *newstr;
  196.   char str1_length = strlen (str1);
  197.  
  198.   newstr = malloc (str1_length + strlen (str2) + 1);
  199.   if (newstr == 0)
  200.     return 0;
  201.   strcpy (newstr, str1);
  202.   strcpy (newstr + str1_length, str2);
  203.   return newstr;
  204. }
  205.  
  206. /* Return NAME with any leading path stripped off.  */
  207.  
  208. char *
  209. basename (name)
  210.      char *name;
  211. {
  212.   char *base;
  213.  
  214.   base = rindex (name, '/');
  215.   return base ? base + 1 : name;
  216. }
  217.  
  218. /* Return the leading directories part of PATH,
  219.    allocated with malloc.  If out of memory, return 0.
  220.    Assumes that trailing slashes have already been
  221.    removed.  */
  222.  
  223. char *
  224. dirname (path)
  225.      char *path;
  226. {
  227.   char *newpath;
  228.   char *slash;
  229.   int length;    /* Length of result, not including NUL. */
  230.  
  231.   slash = rindex (path, '/');
  232.   if (slash == 0)
  233.     {
  234.       /* File is in the current directory.  */
  235.       path = ".";
  236.       length = 1;
  237.     }
  238.   else
  239.     {
  240.       /* Remove any trailing slashes from result. */
  241.       while (slash > path && *slash == '/')
  242.         --slash;
  243.  
  244.       length = slash - path + 1;
  245.     }
  246.   newpath = malloc (length + 1);
  247.   if (newpath == 0)
  248.     return 0;
  249.   strncpy (newpath, path, length);
  250.   newpath[length] = 0;
  251.   return newpath;
  252. }
  253.  
  254. /* If ARG is an unambiguous match for an element of the
  255.    null-terminated array OPTLIST, return the index in OPTLIST
  256.    of the matched element, else -1 if it does not match any element
  257.    or -2 if it is ambiguous (is a prefix of more than one element). */
  258.  
  259. int
  260. argmatch (arg, optlist)
  261.      char *arg;
  262.      char **optlist;
  263. {
  264.   int i;            /* Temporary index in OPTLIST. */
  265.   int arglen;            /* Length of ARG. */
  266.   int matchind = -1;        /* Index of first nonexact match. */
  267.   int ambiguous = 0;        /* If nonzero, multiple nonexact match(es). */
  268.   
  269.   arglen = strlen (arg);
  270.   
  271.   /* Test all elements for either exact match or abbreviated matches.  */
  272.   for (i = 0; optlist[i]; i++)
  273.     {
  274.       if (!strncmp (optlist[i], arg, arglen))
  275.     {
  276.       if (strlen (optlist[i]) == arglen)
  277.         /* Exact match found.  */
  278.         return i;
  279.       else if (matchind == -1)
  280.         /* First nonexact match found.  */
  281.         matchind = i;
  282.       else
  283.         /* Second nonexact match found.  */
  284.         ambiguous = 1;
  285.     }
  286.     }
  287.   if (ambiguous)
  288.     return -2;
  289.   else
  290.     return matchind;
  291. }
  292.  
  293. /* Error reporting for argmatch.
  294.    KIND is a description of the type of entity that was being matched.
  295.    VALUE is the invalid value that was given.
  296.    PROBLEM is the return value from argmatch. */
  297.  
  298. void
  299. invalid_arg (kind, value, problem)
  300.      char *kind;
  301.      char *value;
  302.      int problem;
  303. {
  304.   fprintf (stderr, "patch: ");
  305.   if (problem == -1)
  306.     fprintf (stderr, "invalid");
  307.   else                /* Assume -2. */
  308.     fprintf (stderr, "ambiguous");
  309.   fprintf (stderr, " %s `%s'\n", kind, value);
  310. }
  311.  
  312. static char *backup_args[] =
  313. {
  314.   "never", "simple", "nil", "existing", "t", "numbered", 0
  315. };
  316.  
  317. static enum backup_type backup_types[] =
  318. {
  319.   simple, simple, numbered_existing, numbered_existing, numbered, numbered
  320. };
  321.  
  322. /* Return the type of backup indicated by VERSION.
  323.    Unique abbreviations are accepted. */
  324.  
  325. enum backup_type
  326. get_version (version)
  327.      char *version;
  328. {
  329.   int i;
  330.  
  331.   if (version == 0 || *version == 0)
  332.     return numbered_existing;
  333.   i = argmatch (version, backup_args);
  334.   if (i >= 0)
  335.     return backup_types[i];
  336.   invalid_arg ("version control type", version, i);
  337.   return numbered_existing;
  338. }
  339. #endif /* NODIR */
  340.